// 733. 图像渲染

/**
 * @param {number[][]} image
 * @param {number} sr
 * @param {number} sc
 * @param {number} color
 * @return {number[][]}
 */
const forMap = [
    [-1, 0],
    [1, 0],
    [0, -1],
    [0, 1],
];

var floodFill = function (image, sr, sc, color, passBy = [], initialPoint = image[sr][sc]) {
    image[sr][sc] = color;

    for (let i = 0; i < forMap.length; i++) {
        let x = sr + forMap[i][0];
        let y = sc + forMap[i][1];
        
        if (passBy.some(p => p.x === x && p.y === y)) {
            continue;
        }

        if (x < 0 || y < 0 || x >= image.length || y >= image[0].length) {
            continue;
        }

        if (image[x][y] === initialPoint) {
            passBy.push({x, y});
            floodFill(image, x, y, color, passBy, initialPoint);
        }
    }

    return image;
};

// console.log(
//     floodFill(
//         [
//             [1, 1, 1],
//             [1, 1, 0],
//             [1, 0, 1],
//         ],
//         1,
//         1,
//         2
//     )
// );

console.log(
    floodFill(
        [[0,0,0],[0,0,0]],
        0,
        0,
        0
    )
);
